package com.bawei.detail

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.common.event.CustomEvent
import com.bawei.common.event.LiveDataBus
import com.bawei.common.event.LiveDataBusEvent
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.detail.adapter.DetailMoviePagingAdapter
import com.bawei.detail.databinding.FragmentIntroduceBinding
import com.bawei.detail.entity.AuthorEntity
import com.bawei.detail.intent.DetailViewIntent
import com.bawei.detail.state.DetailUiState
import com.bawei.detail.vm.DetailViewModel
import com.bawei.image.ImgOptions
import com.bawei.image.LoadImageUtil
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_DETAIL_INTRODUCE)
class IntroduceFragment: BaseMviFragment<FragmentIntroduceBinding>() {

    private val mViewModel: DetailViewModel by activityViewModels()
    private lateinit var mPagingDataAdapter: DetailMoviePagingAdapter

    @Autowired
    @JvmField var userId: String = ""

    @Autowired
    @JvmField var videoTypeId: Int = -1


    override fun getLayoutId(): Int = R.layout.fragment_introduce

    override fun initViews() {
        ARouter.getInstance().inject(this)

        mPagingDataAdapter = DetailMoviePagingAdapter()
        mPagingDataAdapter.setItemClickListener(object : DetailMoviePagingAdapter.ItemClickListener{
            override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
                //发送事件给Activity，在其中播放视频
                //方案一： EventBus发事件
                //EventBus.getDefault().post(CustomEvent("DetailActivity", entity, 1))

                //方案二：使用自定义LiveDataBus
                LiveDataBus.with<LiveDataBusEvent>("movie_detail_options").value = LiveDataBusEvent(1, entity)


            }
        })

        mDataBinding.rvUserVideo.apply {
            layoutManager = LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
            adapter = mPagingDataAdapter
        }


        loadMovieList()
    }

    private fun loadMovieList(){
        lifecycleScope.launch {
            if (videoTypeId == -1){
                mViewModel.loadRecommendVideo().collectLatest {
                    mPagingDataAdapter.submitData(it)
                }
            }else{
                mViewModel.loadMutilVideoById(videoTypeId).collectLatest {
                    mPagingDataAdapter.submitData(it)
                }
            }
        }
    }



    override suspend fun sendViewIntent() {
        mViewModel.channel.send(DetailViewIntent.LoadAuthorInfo(userId))
    }


    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is DetailUiState.LoadAuthorInfoSuccess -> setAuthorInfo(it.entity)
                else -> {}
            }
        }
    }


    private fun setAuthorInfo(entity: AuthorEntity){
        LoadImageUtil.loadUrl(requireContext(), entity.avatar_url,  mDataBinding.ivUserIcon, ImgOptions.Builder.isCircle(true).build())
        mDataBinding.tvUserName.text = entity.name
        mDataBinding.tvUserDesc.text = entity.author_desc
        mDataBinding.tvLikeCount.text = entity.followerscount
        mDataBinding.tvSendTime.text = entity.ctime
    }


}